package com.ruantu.clothing.controller;

import com.ruantu.clothing.entity.*;

import com.ruantu.clothing.service.PifashangService;
import com.ruantu.clothing.util.MyException;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

import static java.lang.System.out;

/**
 * Created by Administrator on 2018/6/7 0007.
 */
@Controller
@RequestMapping("/pifashang")
public class PifashangController {
    private static final Logger logger = LoggerFactory.getLogger(PifashangController.class);
    @Autowired
    private PifashangService pifashangService;

    /**
     * 查询所有批发商
     *
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public JsonResponseMessage findAll(Pifashang pifashang, PageBean pageBean, HttpSession session) {
        User user = (User) session.getAttribute("info");
        pifashang.setJgid(user.getJgid());
        pifashang.setLeixing(user.getJgleixing());
        JsonResponseMessage jsonResponseMessage = new JsonResponseMessage();
        jsonResponseMessage.setRows(pifashangService.findList(pifashang, pageBean));
        jsonResponseMessage.setTotal(pageBean.getTotal());
        return jsonResponseMessage;
    }

    /**
     * 导出批发商
     *
     * @return
     */
    @RequestMapping("/downloadexcel")
    @ResponseBody
    public void downExcel(Pifashang pifashang, ServletOutputStream out, HttpServletResponse response, HttpSession session) {
        User user = (User) session.getAttribute("info");
        pifashang.setJgid(user.getJgid());
        pifashang.setLeixing(user.getJgleixing());
        try {
            XSSFWorkbook wb = pifashangService.downExcel(pifashang);
            HttpHeaders headers = new HttpHeaders();
            ByteArrayOutputStream outByteStream = new ByteArrayOutputStream();
            String fileName = URLEncoder.encode("批发商导出数据.xls", "utf-8");
            response.setContentType("application/octet-stream;charset=ISO8859-1");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
            wb.write(out);
            outByteStream.flush();
            outByteStream.close();
        } catch (IOException e) {
            throw new RuntimeException("导出excel失败，请检查网路重试");
        }
    }

    /**
     * 删除批发商
     *
     * @return
     */
    @RequestMapping("/del")
    @ResponseBody
    public JsonResponseMessage delPifashang(Pifashang pifashang) {
        JsonResponseMessage jsonResponseMessage = new JsonResponseMessage();
        try {
            pifashangService.delPifashang(pifashang);
            jsonResponseMessage.setCode(200);
        } catch (MyException e) {
            logger.error(e.getMessage(), e);
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg(e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg(e.getMessage());
        }
        return jsonResponseMessage;
    }


    /**
     * 保存批发商
     *
     * @return
     */
    @RequestMapping("/save")
    @ResponseBody
    public JsonResponseMessage save(Pifashang pifashang, HttpSession session) {
        JsonResponseMessage jsonResponseMessage = new JsonResponseMessage();
        try {
            User user = (User) session.getAttribute("info");
            pifashang.setJgid(user.getJgid());
            pifashang.setCreateBy(user.getId());
            pifashang.setUpdateBy(user.getId());
        } catch (NullPointerException e) {
            logger.error(e.getMessage(), e);
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg("服务器连接已断开，请刷新重试");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg("内部服务器错误，请联系管理员重试");
        }
        try {
            pifashangService.savePifashang(pifashang);
            jsonResponseMessage.setCode(200);
        } catch (MyException e) {
            logger.error(e.getMessage(), e);
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg(e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg("内部服务器错误，请联系管理员重试");
        }
        return jsonResponseMessage;
    }


    /**
     * 查询是否存在批发商
     *
     * @return
     */
    @RequestMapping("/find/pifashang")
    @ResponseBody
    public JsonResponseMessage findGname(Pifashang pifashang, HttpSession session) {
        User user = (User) session.getAttribute("info");
        pifashang.setJgid(user.getJgid());
        JsonResponseMessage jsonResponseMessage = new JsonResponseMessage();
        try {
            pifashangService.findPifashang(pifashang);
            jsonResponseMessage.setCode(200);
        } catch (MyException e) {
            logger.error(e.getMessage(), e);
            jsonResponseMessage.setCode(400);
        }
        return jsonResponseMessage;
    }


    /**
     * 根据id查询修改反显
     *
     * @return
     */
    @RequestMapping("/find/id")
    @ResponseBody
    public JsonResponseMessage findById(Pifashang pifashang, HttpSession session) {
        JsonResponseMessage jsonResponseMessage = new JsonResponseMessage();
        User user = (User) session.getAttribute("info");
        if (user != null) {
            try {
                pifashang.setJgid(user.getJgid());
                jsonResponseMessage.setCode(200);
                jsonResponseMessage.setRows(pifashangService.findPifashangById(pifashang));
            } catch (MyException e) {
                jsonResponseMessage.setCode(400);
                jsonResponseMessage.setMsg(e.getMessage());
                logger.error(e.getMessage(), e);
            } catch (Exception e) {
                jsonResponseMessage.setCode(400);
                jsonResponseMessage.setMsg("服务器内部错误，请联系管理员重试");
                logger.error(e.getMessage(), e);
            }
        } else {
            jsonResponseMessage.setCode(400);
            jsonResponseMessage.setMsg("服务器连接中断，请重新登录进行尝试");
        }
        return jsonResponseMessage;
    }


    /***
     * 根据机构获得所有批发商
     * @param pfs
     * @return
     */
    @RequestMapping("/find/pfslist")
    @ResponseBody
    public JsonResponseMessage findAllPFSList(Pifashang pfs, HttpSession session) {
        JsonResponseMessage msg = new JsonResponseMessage();
        try {
            User u = (User) session.getAttribute("info");
            pfs.setJgid(u.getJgid());
            pfs.setDelFlag(0);
            List<Pifashang> pfsList = pifashangService.getAllLIstPFS(pfs);
            if (pfsList != null) {
                msg.setCode(200);
                msg.setData(pfsList);
                msg.setMsg("获取数据成功");
            } else {
                msg.setCode(1);
                msg.setMsg("暂无数据");
            }
        } catch (Exception e) {
            msg.setCode(400);
            msg.setMsg("服务器内部错误，请联系管理员");
            logger.error(e.getMessage(), e);
        }
        return msg;

    }

    /***
     * 根据机构获得所有批发商
     * @param session
     * @return
     */
    @RequestMapping("/jglist")
    @ResponseBody
    public JsonResponseMessage findAllPFSListForJG(HttpSession session) {
        JsonResponseMessage msg = new JsonResponseMessage();
        try {
            User u = (User) session.getAttribute("info");

            List<Pifashang> pfsList = pifashangService.selectAllPFSForJGLX(u);
            if (pfsList != null) {
                msg.setCode(200);
                msg.setData(pfsList);
                msg.setMsg("获取数据成功");
            } else {
                msg.setCode(1);
                msg.setMsg("暂无数据");
            }
        } catch (Exception e) {
            e.printStackTrace();
            msg.setCode(400);
            msg.setMsg("服务器内部错误，请联系管理员");
            logger.error(e.getMessage(), e);
        }
        return msg;

    }

    /***
     *获得退货客户列表
     * @param session
     * @return
     */
    @RequestMapping("/pfslistth")
    @ResponseBody
    public JsonResponseMessage findAllPFSListForTuiHuo(HttpSession session) {
        JsonResponseMessage msg = new JsonResponseMessage();
        try {
            User u = (User) session.getAttribute("info");
            List<Pifashang> KeHuList = pifashangService.selectallKeHu(u);
            if (KeHuList != null) {
                msg.setCode(100);
                msg.setData(KeHuList);
                msg.setMsg("获得客户成功!");
            } else {
                msg.setCode(1);
                msg.setMsg("暂无数据!");
            }

        } catch (Exception e) {
            e.printStackTrace();
            msg.setCode(400);
            msg.setMsg("服务器内部错误，请联系管理员");
            logger.error(e.getMessage(), e);
        }
        return msg;

    }


    /***
     *获得客户列表:报表用
     * @param u
     * @return
     */
    @RequestMapping("/pfslistthreport")
    @ResponseBody
    public JsonResponseMessage findAllPFSListForTuiHuoReport(User u) {
        JsonResponseMessage msg = new JsonResponseMessage();
        try {
            List<Pifashang> KeHuList = pifashangService.selectallKeHureport(u);
            if (KeHuList != null) {
                msg.setCode(100);
                msg.setData(KeHuList);
                msg.setMsg("获得客户成功!");
            } else {
                msg.setCode(1);
                msg.setMsg("暂无数据!");
            }

        } catch (Exception e) {
            e.printStackTrace();
            msg.setCode(400);
            msg.setMsg("服务器内部错误，请联系管理员");
            logger.error(e.getMessage(), e);
        }
        return msg;

    }


}
